package com.base.system.managedBean;

import java.util.Date;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.Validate;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.base.organization.DAPType;
import com.base.organization.DutyType;
import com.base.organization.entity.DepartmentAndPerson;
import com.base.organization.entity.DepartmentalUnit;
import com.base.organization.entity.Employee;
import com.base.organization.service.IDepartmentAndPersonDAO;
import com.base.organization.service.IDepartmentalUnitDAO;
import com.base.organization.service.IPersonnelDao;
import com.base.security.SecurityConst;
import com.base.security.entity.SecurityRole;
import com.base.security.entity.SecurityUser;
import com.base.security.entity.UserRole;
import com.base.system.managedBean.baseBean.BaseSecurityUserBean;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "system_addSecurityUserBean", scope = ManagedBeanScope.REQUEST)
public class AddSecurityUserBean extends BaseSecurityUserBean {
	Log logger = LogFactory.getLog(AddSecurityUserBean.class);
	private IDepartmentalUnitDAO departmentalUnitDAO = SpringBeanHolder
			.getBean("departmentalUnitDAO");
	private IDepartmentAndPersonDAO departmentAndPersonDAO = SpringBeanHolder
			.getBean("departmentAndPersonDAO");
	private IPersonnelDao personDao = SpringBeanHolder.getBean("personDao");

	@Inject
	private FacesContext context;
	@Bind(id = "departmentId", attribute = "value")
	private String departmentId;

	protected void initSpecialProperty() {

	}

	@Action(id = "saveSecurityUser")
	public void saveSecurityUser() {
		securityUser = new SecurityUser();
		setSecurityUserProperty();
		securityUser.setEnabled(true);
		securityUser.setCreateTime(new Date());
		securityEntityManager.saveUser(securityUser);
		// 加上默认角色
		UserRole ur = new UserRole();
		SecurityRole sr = securityEntityManager
				.getRoleByValue(SecurityConst.ROLE_BASIC);
		ur.setRole(sr);
		ur.setUser(securityUser);
		securityEntityManager.saveUserRole(ur);
		if (departmentId != null && !"".equals(departmentId)
				&& !"null".equals(departmentId)) {
			// 建立部门与人员关系
			DepartmentalUnit departmentalUnit = departmentalUnitDAO.get(Integer
					.valueOf(departmentId));
			DepartmentAndPerson dp = new DepartmentAndPerson();
			dp.setDepartmentalUnit(departmentalUnit);
			dp.setEnable(true);
			dp.setPerson(securityUser);
			dp.setType(DAPType.BELONG);
			dp.setDuty(DutyType.COMMON);
			dp.setIndexNumber(departmentAndPersonDAO.getLastIndexOfDept(Integer
					.valueOf(departmentId)) + 1);
			departmentAndPersonDAO.insert(dp);
			// 生成Employee
			Employee e = new Employee();
			e.setUser(securityUser);
			personDao.insert(e);
		}
		String script = "closeCenterLayoutTab('" + SecurityConst.ADD_TAB + "')";
		Browser.execClientScript("window.top", script);
	}

	@Validate(id = { "userName" }, message = "用户名已经存在，请选择其他名称!")
	public boolean validateUsername(String name) {
		SecurityUser su = securityEntityManager.getUserByUsername(name);
		if (su == null)
			return true;
		else
			return false;
	}
}
